package com.yu.waterstation.system.common.config.mvc;

import com.yu.waterstation.common.common.config.property.IOProperty;
import com.yu.waterstation.system.common.interceptor.SystemApiInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.*;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer, ErrorPageRegistrar {
    @Resource
    private SystemApiInterceptor systemApiInterceptor;  // 后台api拦截器
    @Resource
    private IOProperty ioProperty;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(this.systemApiInterceptor).addPathPatterns("/system/**");
    }


    // 添加静态资源的访问路径处理
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/image/**")
                .addResourceLocations("file:" + ioProperty.getImageFileRootPath());
    }

    // 直接把vue项目打包放到后台时解决找不到页面的问题
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
    }

}
